package cn.stylefeng.guns.config.datasource;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * @Author
 * @date 2020/10/23
 */
@Configuration
@MapperScan(basePackages = {"cn.stylefeng.guns.sys.modular.system.db2.mapper*"}, sqlSessionFactoryRef = "db2SessionFactory")
public class DB2DataSourceConfig {


    @Bean(name = "db2")
    @ConfigurationProperties(prefix = "spring.datasource.druid.db2")
    public DataSource db2() {
        return DruidDataSourceBuilder.create().build();
    }

//    /**
//     * 动态数据源配置
//     *
//     * @return
//     */
//    @Bean
//    @Primary
//    public DataSource multipleDataSource(@Qualifier("db1") DataSource db1,
//                                         @Qualifier("db2") DataSource db2) {
//        DynamicDataSource dynamicDataSource = new DynamicDataSource();
//        Map<Object, Object> targetDataSources = new HashMap<>();
//        targetDataSources.put(DBTypeEnum.db1.getValue(), db1);
//        targetDataSources.put(DBTypeEnum.db2.getValue(), db2);
//        dynamicDataSource.setTargetDataSources(targetDataSources);
//        //默认数据源
//        dynamicDataSource.setDefaultTargetDataSource(db1);
//        return dynamicDataSource;
//    }

    @Bean("db2SessionFactory")
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(db2());
        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:cn/stylefeng/guns/**/mapping/*.xml"));
        sqlSessionFactory.setTypeAliasesPackage("cn.stylefeng.guns.sys.modular.system.db2.entity");
        MybatisConfiguration configuration = new MybatisConfiguration();
        //configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setCacheEnabled(false);
        sqlSessionFactory.setConfiguration(configuration);
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        paginationInterceptor.setDialectType(DbType.POSTGRE_SQL.getDb());


        sqlSessionFactory.setPlugins(new Interceptor[]{
                //PerformanceInterceptor(),OptimisticLockerInterceptor()
                paginationInterceptor
        });
        GlobalConfig conf = new GlobalConfig();
        //new LogicSqlInjector()
        GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
        dbConfig.setLogicDeleteValue("-1");
        dbConfig.setLogicNotDeleteValue("1");
        dbConfig.setIdType(IdType.AUTO);
        conf.setMetaObjectHandler(new MyMetaObjectHandler());
        //dbConfig.setDbColumnUnderline(true);
        //dbConfig.setRefresh(true);
        conf.setDbConfig(dbConfig);
        sqlSessionFactory.setGlobalConfig(conf);
        return sqlSessionFactory.getObject();
    }



}
